import { Label, Node } from "cc";
import { CGameData } from "../../../app/GameDefine";
import { _audio, _gameType, _language, _logic, _platform, _prop, _timer, _ui, AnimtorByTweenCC, ccclass, EBlockOnceAdType, EBottomAdType, EPlatformType, EUILayer, EUIState, NodeHelper, PropHelper, Times } from "../../../main/script/Main";
import { EGameType } from "../../../main/script/module/define/GameTypeDefine";
import { EPropId } from "../../../main/script/module/define/LogicDefine";
import { AnimBaseUI } from "../../../main/script/ui/AnimBaseUI";



@ccclass("RunUI")
export class RunUI extends AnimBaseUI {
	public layerType = EUILayer.WindowUp
	private cLabelTime: Label = null!
	private cLabelLevel: Label = null!
	private cLaytime: Node = null!
	private cLaystep: Node = null!
	private cLabelStep: Label = null!

	private cLayLevel: Node = null!

	private cBPause: Node = null!
	private cNEndBotton: Node = null!
	private cWEndBlack: Node = null!
	private cNBottomBtns: Node = null!

	private cAnimtorByTweenCCTip: AnimtorByTweenCC = null!

	onCreate() {
		super.onCreate()
		this.blockOnceAdType = EBlockOnceAdType.None

		this.addEvent(_gameType.EventType.PLAY_ANIM, () => {
			_audio.play("sceneRes/audio/magic")
			this.cAnimtorByTweenCCTip.setOpacity(1)
			this.cAnimtorByTweenCCTip.play()
		}, this, _gameType)
		this.addEvent(PropHelper.EventType.CHANGE, this.onEventStepHandler, this, _logic._nuoLevel.step)
		this.addEvent(_logic.EventType.DURATION_CHANGE, this.onLoopDownCountHandler, this, _logic)
		this.addEvent(_logic.EventType.FAIL, () => {
			this.setEnd(true)
		}, this, _logic)

		this.addEvent(_prop.EventType.PROP_SUB, (id: number) => {
			switch (id) {
				case EPropId.reset:
					this.setEnd(false)
					break
				case EPropId.remove:
					this.setEnd(false)
					break
			}
		}, this, _prop)

	}

	onOpen() {
		super.onOpen()
		this.updateBottomAdType()
		this.cAnimtorByTweenCCTip.setOpacity(0)
		_timer.clear(this, this.onLoopDownCountHandler)
		this.setEnd(false)

		switch (_gameType.type) {
			case EGameType.tenLevel:
				this.playAnim(true)
				this.onEventLevel(_logic._tenLevel.curLevel)
				break
			case EGameType.nuoLevel:
				this.playAnim(true)
				this.onEventLevel(_logic._nuoLevel.curLevel)
				this.onEventStepHandler()
				break
			case EGameType.nuoToday:
				if (_logic._nuoToday.configIndex == 0) this.playAnim(true)
				_timer.loop(this, this.onLoopDownCountHandler, 1, -1, -1)
				break
		}

		let bottonY = _platform.isLongScreen && !_logic.isTenGameType ? 140 : 0
		NodeHelper.setPositionY(this.cNBottomBtns, bottonY, false)

	}

	private updateBottomAdType() {
		if (_platform.isLongScreen) {
			if (_logic.isTenGameType) {
				this.bottomAdType = EBottomAdType.None
			} else {
				switch (_platform.type) {
					case EPlatformType.wx:
						this.bottomAdType = EBottomAdType.Native
						break
					case EPlatformType.web:
						this.bottomAdType = EBottomAdType.None
						break
					default:
						this.bottomAdType = EBottomAdType.Banner
						break
				}
			}
		} else {
			this.bottomAdType = EBottomAdType.None
		}

	}

	private setEnd(value: boolean) {
		this.cNEndBotton.active = value
		this.cWEndBlack.active = value

		if (value) {
			this.cLaytime.active = !value
			this.cLaystep.active = !value
			this.cLayLevel.active = !value
			this.cBPause.active = !value
		} else {
			this.cBPause.active = true
			this.cLayLevel.active = _gameType.type == EGameType.tenLevel || _gameType.type == EGameType.nuoLevel
			this.cLaytime.active = _gameType.type == EGameType.nuoToday
			this.cLaystep.active = _gameType.type == EGameType.nuoLevel
		}
	}

	private onClickAEnd() {
		this.setEnd(false)
		_gameType.runComplete(false)
	}

	private onEventLevel(level: number) {
		this.cLabelLevel.string = _language.replace(40014, level + "")
	}

	private onEventStepHandler() {
		let cur = _logic._nuoLevel.step.cur
		this.cLabelStep.string = cur + ""
	}


	private onLoopDownCountHandler() {
		let checkUrl = [CGameData.SettingUrl, CGameData.SuccessUrl, CGameData.FailUrl]
		for (let index = 0; index < checkUrl.length; index++) {
			let _state = _ui.getModule(checkUrl[index]).state;
			if (_state == EUIState.Open || _state == EUIState.Load)
				return
		}
		let sub = _logic._nuoToday.intervalTimeCountDown.sub * 1000
		if (this.cLabelTime)
			this.cLabelTime.string = Times.numHourCountDown(sub)
	}

}
